<?php

namespace app\admin\controller;

use DateTime;
use think\facade\Db;
use think\facade\View;
use utils\JsonHandle;

class Content extends BaseController
{
    //页面跳转
    public function index()
    {
        return View::fetch();
    }

    //后台用户主页面
    public function listPage()
    {
        return View::fetch("list");
    }

    public function savePage()
    {
        return View::fetch("save");
    }

    public function detailPage()
    {
        $id = request()->param('id');
        $data = null;
        if (!empty($id)) {
            $data = \app\admin\model\Content::find($id);
        }
        return View::fetch("detail", ['content' => $data]);
    }

    //游客查看主页面
    public function lookPage()
    {
        return View::fetch("look");
    }

    //编辑页面 并传递参数
    public function editPage()
    {
        $id = request()->param('id');
        $data = null;
        if (!empty($id)) {
            $data = \app\admin\model\Content::find($id);
        }
        return view('edit', ['content' => $data]);
    }

    /**
     * 上传文件
     * @return \think\response\Json
     * "code": 200 //200表示成功，其它失败
     * ,"msg": "" //提示信息 //一般上传失败后返回
     * ,"data": {
     * "src": "图片路径"
     * ,"title": "图片名称" //可选
     * }
     */
    public function upload()
    {
        //获取上传的文件
        $file = request()->file('file');
        //判断 是否上传了文件
        if (empty($file)) {
            return JsonHandle::fail('没有上传文件!!');
        }
        try {
//            validate(['image' => 'filesize:10485760|fileExt:jpg,png,gif,jpeg'])->check($file);
            $savename = \think\facade\Filesystem::disk('public')->putFile('uploads', $file);
            $path = '/storage/' . $savename;
            $data = [
                'src' => $path,
                'title' => "图片",
            ];
            return JsonHandle::ok($data);
        } catch (\think\exception\ValidateException $e) {
            return JsonHandle::fail($e->getMessage());
        }
    }

    public function save()
    {
        //获取参数
        $params = request()->param('contentForm');
        //更新
        if (!empty($params["id"])) {
            $params["update_time"] = $this->getDateTime();
            \app\admin\model\Content::update($params);
            return JsonHandle::ok();
        }
        $content = new \app\admin\model\Content($params);
        $content->save();
        return JsonHandle::ok();
    }

    //列表 _分页
    public function list()
    {
        //获取参数
        $params = input();
        $page = 1;
        $limit = 10;
        if (isset($params["page"])) {
            $page = ($params["page"]);
        }
        if (isset($params["limit"])) {
            $limit = ($params["limit"]);
        }
        $data = Db::table('t_content')->withoutField('content')
            ->when(isset($params["title"]), function ($query) use ($params) {
                // 满足条件后执行
                $query->where("title", "like", "%" . ($params["title"]) . "%");
            })->when(isset($params["type"]), function ($query) use ($params) {
                // 满足条件后执行
                $query->where("type", "like", "%" . $params["type"] . "%");
            })
            ->where("status", "<>", 3)->order("update_time", "DESC")
            ->page($page, $limit)->select();
        //总条数
        $total = Db::table('t_content')
            ->when(isset($params["title"]), function ($query) use ($params) {
                // 满足条件后执行
                $query->where("title", "like", "%" . ($params["title"]) . "%");
            })->when(isset($params["type"]), function ($query) use ($params) {
                // 满足条件后执行
                $query->where("type", "like", "%" . $params["type"] . "%");
            })
            ->where("status", "<>", 3)
            ->count("id");
        return JsonHandle::ok($data, 200, $total);
    }

    //软删除
    public function delete()
    {
        //获取参数
        $params = input();
        try {
            Db::name('t_content')
                ->where('id', $params["id"])
                ->update(['status' => 3, 'update_time' => $this->getDateTime()]);
            return JsonHandle::ok();
        } catch (\Exception $e) {
            return JsonHandle::fail();
        }
    }

    //获取时间
    private function getDateTime()
    {
        try {
            return (new DateTime())->format('Y-m-d H:i:s');
        } catch (\Exception $e) {
            return null;
        }
    }

    //前台列表 _分页_搜索
    public function look_list()
    {
        //获取参数
        $params = input();
        $page = 1;
        $limit = 10;
        if (isset($params["page"])) {
            $page = ($params["page"]);
        }
        if (isset($params["limit"])) {
            $limit = ($params["limit"]);
        }
        $data = Db::table('t_content')->withoutField('content')
            ->where("status", "=", 2)
            ->when(isset($params["title"]), function ($query) use ($params) {
                // 满足条件后执行
                $query->where("title", "like", "%" . ($params["title"]) . "%");
            })->when(isset($params["type"]), function ($query) use ($params) {
                // 满足条件后执行
                $query->where("type", "like", "%" . $params["type"] . "%");
            })
            ->order("update_time", "DESC")
            ->page($page, $limit)->select();
        //总条数
        $total = Db::table('t_content')
            ->where("status", "=", 2)
            ->when(isset($params["title"]), function ($query) use ($params) {
                // 满足条件后执行
                $query->where("title", "like", "%" . ($params["title"]) . "%");
            })->when(isset($params["type"]), function ($query) use ($params) {
                // 满足条件后执行
                $query->where("type", "like", "%" . $params["type"] . "%");
            })
            ->count("id");
        return JsonHandle::ok($data, 200, $total);
    }
}
